package com.tbycoder.tby.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tbycoder.tby.entity.IposResouces;
import com.tbycoder.tby.entity.IposResoucesFile;
import com.tbycoder.tby.entity.Request.CommonIdRequest;
import com.tbycoder.tby.entity.Request.GetFileByResourceRequest;
import com.tbycoder.tby.entity.Vo.FileVo;
import com.tbycoder.tby.entity.Vo.ResourceVo;
import com.tbycoder.tby.mapper.IposLoginMapper;
import com.tbycoder.tby.mapper.IposResoucesFileMapper;
import com.tbycoder.tby.mapper.IposResoucesMapper;
import com.tbycoder.tby.service.IIposResoucesFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbycoder.tby.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tby
 * @since 2021-04-14
 */
@Service
public class IposResoucesFileServiceImpl extends ServiceImpl<IposResoucesFileMapper, IposResoucesFile> implements IIposResoucesFileService {

    @Resource
    IposResoucesFileMapper iposResoucesFileMapper;
    @Resource
    IposResoucesMapper iposResoucesMapper;
    @Resource
    IposLoginMapper iposLoginMapper;
    @Value("${file.domain}")
    String domain;
    @Override
    public void uploadmajorfile(FileVo fileVo,String resources) {
        String userId = TokenUtil.getUserId();
//        String identityId = iposLoginMapper.selectOne(
//                Wrappers.lambdaQuery(IposLogin.class).eq(IposLogin::getUserId,userId)
//        ).getIdentityId();
//        String fileId = qzzpPicMapper.selectOne(
//                Wrappers.lambdaQuery(QzzpPic.class).eq(QzzpPic::getUserId,userId)
//        ).getPicId();
//        if(fileId != null){
//            qzzpPicFileMapper.delete(
//                    Wrappers.lambdaQuery(QzzpPicFile.class).eq(QzzpPicFile::getSysFileId,fileId)
//            );
//        }
//        qzzpPicMapper.update(null,
//                Wrappers.lambdaUpdate(QzzpPic.class).eq(QzzpPic::getUserId,userId)
//                        .eq(QzzpPic::getIdentityId,identityId)
//                        .set(QzzpPic::getPicId,fileVo.getFileId()));
//        qzzpPicFileMapper.insert(
//                QzzpPicFile.builder()
//                        .sysFileId(fileVo.getFileId())
//                        .sysPath(fileVo.getFileName())
//                        .name(fileVo.getOriginName())
//                        .build()
//        );
        String resourceId = iposResoucesMapper.selectOne(Wrappers.lambdaQuery(IposResouces.class)
                .eq(IposResouces::getResouces,resources)).getResoucesId();
        iposResoucesFileMapper.insert(
                IposResoucesFile.builder()
                                .userId(userId)
                                .sysFileId(fileVo.getFileId())
                                .resoucesId(resourceId)
                                .sysPath(fileVo.getFileName())
                                .sysCreateTime(LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()))
                                .name(fileVo.getOriginName())
                .build()
        );
    }

    @Override
    public void modifymajorfile(CommonIdRequest fileId) {
        iposResoucesFileMapper.delete(Wrappers.lambdaQuery(IposResoucesFile.class).eq(IposResoucesFile::getSysFileId,fileId.getId()));
    }

    @Override
    public String getUrl(CommonIdRequest commonIdRequest) {
        IposResoucesFile iposResoucesFile = iposResoucesFileMapper.selectOne(
                Wrappers.lambdaQuery(IposResoucesFile.class).eq(IposResoucesFile::getSysFileId,commonIdRequest.getId())
        );
        String url = "";
        if (iposResoucesFile != null) {
            url += domain + "/" + iposResoucesFile.getSysPath();
        }
        return url;
    }

    @Override
    public Page<ResourceVo> getfileByResource(GetFileByResourceRequest getFileByResourceRequest) {
        Page<IposResoucesFile> iposResoucesFilePage = new Page<>();
        iposResoucesFilePage.setCurrent(getFileByResourceRequest.getPageRequest().getCurrentPage());
        iposResoucesFilePage.setSize(getFileByResourceRequest.getPageRequest().getPageSize());
        Page<ResourceVo> resourceVoPage = new Page<>();
        List<ResourceVo> resourceVos = new ArrayList<>();
        String resourceId = iposResoucesMapper.selectOne(Wrappers.lambdaQuery(IposResouces.class)
                .eq(IposResouces::getResouces,getFileByResourceRequest.getResource())).getResoucesId();
        List<IposResoucesFile> records = iposResoucesFileMapper.selectPage(iposResoucesFilePage, Wrappers.lambdaQuery(IposResoucesFile.class)
                .eq(IposResoucesFile::getResoucesId, resourceId)).getRecords();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for(IposResoucesFile iposResoucesFile : records){
            ResourceVo resourceVo = new ResourceVo();
            resourceVo.setResourceId(iposResoucesFile.getSysFileId());
            resourceVo.setResourceFileName(iposResoucesFile.getName());
            resourceVo.setTime(df.format(iposResoucesFile.getSysCreateTime()));
            resourceVos.add(resourceVo);
        }
        resourceVoPage.setRecords(resourceVos);
        return resourceVoPage;
    }
}
